﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Bluebunnie.Library.Transaction.ProjectDataSetTableAdapters;
using Bluebunnie.Extensions;

namespace Bluebunnie.Library.Transaction
{
    /// <summary>
    /// Interaction logic for SaleTransaction.xaml
    /// </summary>
    public partial class SaleTransaction : PulseControl
    {
        tbl_sale_transactionTableAdapter adapter = new tbl_sale_transactionTableAdapter();
        tbl_sale_orderTableAdapter orderAdapter = new tbl_sale_orderTableAdapter();
        tbl_userTableAdapter userAdapter = new tbl_userTableAdapter();
        tbl_customerTableAdapter custAdapter = new tbl_customerTableAdapter();
        tbl_salesTableAdapter salesAdapter = new tbl_salesTableAdapter();
        tbl_itemTableAdapter itemAdapter = new tbl_itemTableAdapter();

        public SaleTransaction()
        {
            InitializeComponent();

            Save = btnKeluar_Click;

            try
            {
                Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Tidak dapat mengambil data. Pastikan server MySQL telah berjalan.\n\nError:\n{0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnKeluar_Click(object sender, RoutedEventArgs e)
        {
            ProjectDataSet dataset = (ProjectDataSet)FindResource("projectDataSet");
            adapter.Update(dataset.tbl_sale_transaction);
        }

        public override void Refresh()
        {
            base.Refresh();

            ProjectDataSet dataset = (ProjectDataSet)FindResource("projectDataSet");
            adapter.Fill(dataset.tbl_sale_transaction);
            orderAdapter.Fill(dataset.tbl_sale_order);
            userAdapter.Fill(dataset.tbl_user);
            custAdapter.Fill(dataset.tbl_customer);
            salesAdapter.Fill(dataset.tbl_sales);
            itemAdapter.Fill(dataset.tbl_item);

            CollectionViewSource source = (CollectionViewSource)FindResource("tbl_sale_transactionViewSource");
            source.View.MoveCurrentToFirst();
        }
    }
}
